//
//  ReplyHeaderCell.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/8/29.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
protocol ReplyHeaderCellDelegate {
    func checkZanPersonNumber()
    func zanSelected()
}
class ReplyHeaderCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
    var modelList: allReplyInfo?{
        didSet{
            self.setUpView()
            self.collection.reloadData()
        }
    }
    var delegate:ReplyHeaderCellDelegate?
    var cellHeight: CGFloat  = 0.0
    var type: String = ""//分为活动和行业
    @IBOutlet weak var iv: UIImageView!
    @IBOutlet weak var idLbl: UILabel!
    @IBOutlet weak var signLbl: UILabel!
    @IBOutlet weak var messageLbl: UILabel!
    @IBOutlet weak var collection: UICollectionView!
    @IBOutlet weak var zanNumLbl: UILabel!
    @IBOutlet weak var zanBtn: UIButton!
    @IBOutlet weak var imgZanNum: UILabel!
    
    
    
    @IBOutlet weak var messageCons: NSLayoutConstraint!
    override func awakeFromNib() {
        super.awakeFromNib()
        self.collection.dataSource = self
        self.collection.delegate = self
        self.collection .registerNib(UINib (nibName: "JoinUsWantJobMiddleCollCell", bundle: nil), forCellWithReuseIdentifier: "JoinUsWantJobMiddleCollCell")
        
        self.collection.scrollEnabled = false
    }
    func setCellContentWithCellInfo(cellInfo: String) -> Void {
        
        let font = UIFont(name: "Helvetica", size: 16)
        
        if self.modelList == nil{
            cellHeight = 0
        }else{
            cellHeight = publicMethod.getLabHeigh((cellInfo), font:font!, width:SCREEN_WIDTH - 16)
            
        }

        self.messageCons.constant = cellHeight
    }
    //MARK:UICollectionViewDataSource
    //每一组有多少个cell
    func collectionView(collectionView:UICollectionView,numberOfItemsInSection section: Int) -> Int{
        
        if modelList?.userdzList.count == nil{
            return 0
        }else{
            return (modelList?.userdzList.count)!
        }
        
        
    }
    //设置每组的cell的边界
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        
        return UIEdgeInsetsMake(5, 5, 5, 5)//上左下右
    }
    //最小行间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 5
    }
    //最小列间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 5
    }
    //设定全局的Cell尺寸
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        
        return CGSizeMake(SCREEN_WIDTH/7-20,40)
        
    }
    //cell上显示的信息
    func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) ->UICollectionViewCell{
        
        let cell = collection.dequeueReusableCellWithReuseIdentifier("JoinUsWantJobMiddleCollCell", forIndexPath: indexPath) as! JoinUsWantJobMiddleCollCell
        
        let model = modelList!.userdzList[indexPath.row]
        
        cell.iv.sd_setImageWithURL(NSURL(string: model.timg), placeholderImage: UIImage(named: "placeholder"))
        
        return cell
        
        
    }
    @IBAction func zanPersonBtnClicked(sender: UIButton){
        delegate?.checkZanPersonNumber()
    }
    @IBAction func zanBtnClicked(sedner: UIButton){
        if ApplicationDelegate.isLogin(){
            if self.modelList?.isdianzan == true{
                SVProgressHUD.showErrorWithStatus("您已经赞过")
            }else{
                DataTool.supposeComment(Int(self.modelList!.id)!, type: self.type,  completionHandler: { (isSuccess) -> Void in
                    if isSuccess {
                        let value = Int(self.modelList!.suppose)! + 1
                        
                        self.zanNumLbl.text = String(value)
                        self.zanBtn.setImage(UIImage(named: "zan_selected"), forState: UIControlState.Normal)
                    } else {
                        print("网络错误")
                    }
                })
            }
        }else{
            SVProgressHUD.showInfoWithStatus("请先登录")
        }
        
        

    }
    func setUpView(){
        if self.modelList?.isdianzan == true{
            self.zanBtn.setImage(UIImage(named: "zan_selected"), forState: UIControlState.Normal)
        }else{
            self.zanBtn.setImage(UIImage(named: "zan"), forState: UIControlState.Normal)
        }
        self.iv.sd_setImageWithURL(NSURL(string: (self.modelList!.timg)), placeholderImage: UIImage(named: "placeholder"))
        self.idLbl.text = self.modelList?.name
        self.signLbl.text = publicMethod.computationTimeDifference((self.modelList?.commenttime)!)
        self.messageLbl.text = self.modelList?.message
        self.zanNumLbl.text = self.modelList?.suppose
        self.imgZanNum.text = String(format: "%@人赞过",(self.modelList?.suppose)!)
    }


       
}
